<?php

include('pagina.php');
include('database.php');
include('auth.php');
include('core.php');

// Verifica se tem permissao
validaPermissao(3);

// Verifica se ha supply para ver
if (!isset($_GET['id']) && !isset($_POST['id']))
{
	forbidden();
}
$supply_id = (isset($_GET['id'])? $_GET['id'] : $_POST['id']);

// Busca todos os dados do supply
$supply = query_fetch("SELECT * FROM supplies WHERE id='{$supply_id}' LIMIT 1");

// Se nao existe, cai fora
if (!$supply)
{
    response_404();
}

// Verifica acao
if (isset($_GET['remove']))
{
	query_bd("DELETE FROM supplies WHERE id={$supply_id}");
	header( 'Location: supplies.php?msg=The previous supply item was just been removed from the system.' );
	die();
}

// Verifica se houve entrada no formulario
if (isset($_POST['add']))
{
	$new_qtd = $supply['quantity'] + $_POST['quantity'];
	query_bd("UPDATE supplies SET quantity={$new_qtd} WHERE id={$supply_id}");
	$supply['quantity'] = $new_qtd;
}
// Verifica se houve entrada no formulario
else if (isset($_POST['remove']))
{
	$new_qtd = $supply['quantity'] - $_POST['quantity'];
	query_bd("UPDATE supplies SET quantity={$new_qtd} WHERE id={$supply_id}");
	$supply['quantity'] = $new_qtd;
}

// Registra como income ou expense
if (@$_POST['register_as'] == 'income')
{
	header( "Location: income_new.php?name={$supply['name']}&description={$supply['description']}&value=" .
		($_POST['quantity'] * $supply['value']) .
		"&msg=<a href='supply.php?id={$supply['id']}'><span><img src='img/back.gif' />Back to item</span></a>" );
	exit;
}
else if (@$_POST['register_as'] == 'expense')
{
	header( "Location: expense_new.php?name={$supply['name']}&description={$supply['description']}&value=" .
		($_POST['quantity'] * $supply['value']) .
		"&msg=<a href='supply.php?id={$supply['id']}'><span><img src='img/back.gif' />Back to item</span></a>" );
	exit;
}

// Inicia cabecalho da pagina
pagina_inicio('ParkSys - Supply');
adicionar_css('supply.css');
adicionar_js('input-format.js');
pagina_head();

?>
<script type='text/javascript' language='JavaScript'>

function validateAdd()
{
	var qtd = document.getElementById('add_remove_supply').quantity.value;

	if (qtd == 0)
	{
		alert("You must input some quantity to add");
		return false;
	}
	return true;
}

function validateRemove()
{
	var qtd = document.getElementById('add_remove_supply').quantity.value;

	if (qtd == 0)
	{
		alert("You must input some quantity to remove");
		return false;
	}
	else if (qtd > <?php echo $supply['quantity'] ?>)
	{
		alert("You can not remove more items than you have");
		return false;
	}
	return true;
}

</script>
<?php

// Mostra opcoes
echo "<ul id=opcoes>";
echo "<li><a href='supply_edit.php?id={$supply['id']}'><img src='img/edit.png' alt=''>Edit supply item</a></li>";
echo "<li class='negative'><a href='supply.php?id={$supply['id']}&amp;remove=1' ",
	"onClick='return confirm(\"Do you really want to remove this item?\");'>",
	"<img src='img/cancel.png' alt=''>Remove supply item</a></li>";
echo "<li><a href='supply.php?print=1&amp;id={$supply['id']}' target='_blank'>",
	"<img src='img/print.png' alt=''>Print</a></li>";
echo "</ul><br>";

echo "<div class=dados_estaticos>";
echo "<h1>Supply item</h1><br>";

echo "<dl>";
echo "<dt>Item name</dt><dd>{$supply['name']}</dd>";
echo "<dt>Description</dt><dd>{$supply['description']}</dd>";
echo "<dt>Category</dt><dd>" . $SUPPLIES_CATEGORIES[$supply['category']] . "</dd>";
echo "<dt>Item price(each)</dt><dd>\${$supply['value']}</dd>";
echo "<dt>Quantity in stock</dt><dd>{$supply['quantity']}";

echo "<form id=add_remove_supply method=POST action='supply.php'>";
echo "<input type=HIDDEN name=id value={$supply['id']} >";
echo "<input type=TEXT name=quantity value='0' size=5 onChange='format_int(this)'><br>";
echo "Register as: <select name='register_as'>";
echo "<option value='none'>Do not register</option>";
echo "<option value='income'>Income</option>";
echo "<option value='expense'>Expense</option>";
echo "</select><br>";
echo "<input type=SUBMIT name=add value=Add onClick='return validateAdd()'> or " .
	"<input type=SUBMIT name=remove value=Remove onClick='return validateRemove()'><br>";
echo "</form></dd>";

if ($supply['obs'])
	echo "<dd><hr></dd><dt>Observation</dt><dd>\${$supply['obs']}</dd>";
echo "</dl>";
echo "</div>";

// Finaliza pagina
pagina_fim();
?>
